package com.qzdatasoft.mcp.server.demo.tool;

import com.qzdatasoft.mcp.server.common.IMcpToolProvider;
import com.qzdatasoft.mcp.server.common.McpFactory;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.function.Function;

@Service
public class MyToolsProvider implements IMcpToolProvider {

    /// ///////////////////////////////////
    // tools definitions
    @Tool(description = "Calculator Add for two numbers")
    public Integer add(@ToolParam(description = "first number") int a,
                       @ToolParam(description = "second number") int b) {

        return a + b;
    }

    @Tool(description = "Get current time")
    public LocalDateTime getCurrentTime() {
        return LocalDateTime.now();
    }

    /// ///////////////////////////////////
    // record ToUpperCaseInput
    public record ToUpperCaseInput(String input) { }

    static {
        //////////////////////////////////////
        // 向 toolCallbacks 动态增加一个 toUpperCase 方法
        FunctionToolCallback<ToUpperCaseInput, String> upperCaseTool =
                FunctionToolCallback.builder("toUpperCase", (Function<ToUpperCaseInput, String>) input -> input.input().toUpperCase())
                        .description("Convert string to uppercase")
                        .inputType(ToUpperCaseInput.class)
                        .build();
        McpFactory.getToolCallbackList().add(upperCaseTool);

        log.info("initialized dynamic tool toUpperCase at {}", LocalDateTime.now());

    }

}

